import React, { useState } from 'react'
import { Table } from 'antd'
import { ColumnProps } from 'antd/lib/table'
import ItemModal from '~/components/item-modal'

let cacheRecord: any = {}

const colums = ({ setModalVisible }: any): ColumnProps<any>[] => [
  {
    title: '序号',
    dataIndex: 'id',
    align: 'center',
    render: (_: any, record: any, index: number) => <div>{index + 1}</div>
  },
  {
    title: '物品编号',
    dataIndex: 'serialNo',
    align: 'center',
    render: (serialNo: any, record: any) => (
      <a
        onClick={() => {
          cacheRecord = record
          setModalVisible(true)
        }}
      >
        {serialNo}
      </a>
    )
  },
  {
    title: '物品名称',
    dataIndex: 'itemName',
    align: 'center',
    render: (itemName: any, record: any) => (
      <a
        onClick={() => {
          cacheRecord = record
          setModalVisible(true)
        }}
      >
        {itemName}
      </a>
    )
  },
  {
    title: '数量（单位）',
    dataIndex: 'itemStock',
    align: 'center',
    render: (itemName: any, record: any) => (
      <div>
        {record.itemStock.totalNumber} ({record.itemStock.unitName})
      </div>
    )
  },
  {
    title: '规格',
    dataIndex: 'specs',
    align: 'center',
    key: 'specs'
  },
  {
    title: '库架编号',
    dataIndex: 'storeLocationNo',
    align: 'center',
    key: 'storeLocationNo'
  },
  {
    title: '特征',
    dataIndex: 'feature',
    align: 'center',
    key: 'feature'
  },
  {
    title: '备注',
    dataIndex: 'remark',
    align: 'center',
    key: 'remark'
  },
  {
    title: '借用数量',
    dataIndex: 'borrowNumber',
    align: 'center',
    render: (itemName: any, record: any) => <div>{record.itemStock.borrowNumber}</div>
  }
]

const WaitingList = ({ waitCastItems }: any) => {
  const [modalVisible, setModalVisible] = useState(false)
  return (
    <div>
      <Table
        pagination={{
          size: 'default',
          total: waitCastItems.length,
          showSizeChanger: true,
          showQuickJumper: true,
          showTotal: total => `共 ${total} 条`
        }}
        rowKey="serialNo"
        dataSource={waitCastItems}
        columns={colums({ setModalVisible })}
        bordered
      />
      <ItemModal visible={modalVisible} setVisible={setModalVisible} record={cacheRecord} />
    </div>
  )
}

export default WaitingList
